#ifndef _LED_H_
#define _LED_H_

#include "Global.h"

#define _COM_ANODE_   (0)     // 共阳

#define P_LED_COM0  RB3
#define P_LED_COM1  RB4
// #define P_LED1      RA0
// #define P_LED2      RA1
// #define P_LED3      RA2
// #define P_LED4      RA3
// #define P_LED5      RA4

#define P_LED1    DEF_PIN(A,0)
#define M_LED1    DEF_PIN_DIR(A, 0)
#define WPU_LED1  DEF_PIN_WPU(A, 0)

#define P_LED2    DEF_PIN(A,1)
#define M_LED2    DEF_PIN_DIR(A, 1)
#define WPU_LED2  DEF_PIN_WPU(A, 1)

#define P_LED3    DEF_PIN(A,2)
#define M_LED3    DEF_PIN_DIR(A, 2)
#define WPU_LED3  DEF_PIN_WPU(A, 2)

#define P_LED4    DEF_PIN(A,3)
#define M_LED4    DEF_PIN_DIR(A, 3)
#define WPU_LED4  DEF_PIN_WPU(A, 3)

#define P_LED5    DEF_PIN(A,4)
#define M_LED5    DEF_PIN_DIR(A, 4)
#define WPU_LED5  DEF_PIN_WPU(A, 4)
 
#if _COM_ANODE_
#define OPEN_LED_COM()  do{PORTB |= 0b00011000;}while(0)
#define CLOSE_LED_COM() do{PORTB &= ~0b00011000;}while(0)
#define EN_LED1()       P_LED1 = LOW
#define DIS_LED1()      P_LED1 = HIGH
#define EN_LED2()       P_LED2 = LOW
#define DIS_LED2()      P_LED2 = HIGH
#define EN_LED3()       P_LED3 = LOW
#define DIS_LED3()      P_LED3 = HIGH
#define EN_LED4()       P_LED4 = LOW
#define DIS_LED4()      P_LED4 = HIGH
#define EN_LED5()       P_LED5 = LOW
#define DIS_LED5()      P_LED5 = HIGH
#else
#define CLOSE_LED_COM()  do{PORTB |= 0b00011000;}while(0)
#define OPEN_LED_COM() do{PORTB &= ~0b00011000;}while(0)
#define EN_LED1()       P_LED1 = HIGH
#define DIS_LED1()      P_LED1 = LOW
#define EN_LED2()       P_LED2 = HIGH
#define DIS_LED2()      P_LED2 = LOW
#define EN_LED3()       P_LED3 = HIGH
#define DIS_LED3()      P_LED3 = LOW
#define EN_LED4()       P_LED4 = HIGH
#define DIS_LED4()      P_LED4 = LOW
#define EN_LED5()       P_LED5 = HIGH
#define DIS_LED5()      P_LED5 = LOW
#endif

#define SET_IO_LED_MODE()   do{TRISA &= ~0b00011111; WPUA &= ~0b00011111;}while(0)
#define SET_IO_KEY_MODE()   do{PORTA |= 0b00011111; TRISA |= 0b00011111; WPUA |= 0b00011111;}while(0)

extern volatile uint16 TimeLedOn;


enum
{
    LED1 = 0x01,
    LED2 = 0x02,
    LED3 = 0x04,
    LED4 = 0x08,
    LED5 = 0x10,
};

#define ALL_ON      0xFF
#define ALL_OFF     0x00
#define BLINK_SEND  LED5

#define LED_PAIR        (LED1+LED2+LED3)
#define LED_SEND        LED5
#define TIME_BASE_LED   8
#define TM_LED_SHORT    (600/TIME_BASE_LED)
#define TM_LED_DISP     (3000/TIME_BASE_LED)
#define TM_LED_MODE     (1000/TIME_BASE_LED)
#define TM_LED_LONG     (5000/TIME_BASE_LED)
#define TM_LED_POWER_ON (1000/TIME_BASE_LED)
#define TM_LED_PAIR     (15000ul/TIME_BASE_LED)

extern uint8 RLedBuf;
extern bit F_LED_SEND;
extern const uint8 TAB_LED[];

void    initLED(void);
void    SetLed(uint8 led_buf,uint16 time_on);
void    Led_Main(void);

#endif
